package apis

import (
	"net/http"
	"github.com/gin-gonic/gin"
	"hulk-api/models/k8s"
	"fmt"
	"io/ioutil"

)


func GetK8sNodes(c *gin.Context)  {
	nodes,_ := k8s.GetKubernetesNodes()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"nodes":nodes,
	})
}

func GetK8sNodeDetail(c *gin.Context)  {
	nodename := c.Query("name")

	detail,events,_ := k8s.GetKubernetesNodeDetail(nodename)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"detail":detail,
		"events":events,
	})
}


func GetK8sNamespaces(c *gin.Context)  {
	ns,_ := k8s.GetKubernetesNamespaces()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"namespaces":ns,
	})
}

func GetK8sNamespaceDetail(c *gin.Context)  {
	ns := c.Query("name")
	fmt.Println(ns)
	deployments,services,events,configmaps,secrets,jobs,statefulesets,pvcs,ds,_ := k8s.GetKubernetesNamespaceDetail(ns)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"deployments":deployments,
		"services":services,
		"events":events,
		"configmaps":configmaps,
		"secrets":secrets,
		"jobs":jobs,
		"statefulesets":statefulesets,
		"pvcs":pvcs,
		"daemonsets":ds,

	})
}

func GetK8sDeploymentDetail(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")

	deployDetail,newReplicaSets,oldReplicaSets,hpas,events,_ := k8s.GetKubernetesDeploymentDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"deployDetail":deployDetail,
		"newReplicaSets":newReplicaSets,
		"oldReplicaSets":oldReplicaSets,
		"hpas":hpas,
		"events":events,
	})
}

func GetK8sReplicaSetDetail(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")

	rs,pods,services,events,_ :=k8s.GetKubernetesReplicaSetDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"replicaset":rs,
		"pods":pods,
		"events":events,
		"services":services,

	})
}

func GetK8sPodDetail(c *gin.Context)  {
	ns := c.Query("namespace")
	podName := c.Query("name")


	pods,events,_ := k8s.GetKubernetesPodDetail(ns,podName)

	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"pods":pods,
		"events":events,

	})

}


func DeleteK8sPod(c *gin.Context)  {
	ns := c.Query("namespace")
	podName := c.Query("name")
	k8s.DeleteKubernetesPod(ns,podName)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
	})

}

func GetK8sPodMetadata(c *gin.Context)  {
	ns := c.Query("namespace")
	podName := c.Query("name")
	pod,_ := k8s.GetKubernetesPodMetadata(ns,podName)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"pod":pod,
	})

}

func UpdateK8sPodMetadata(c *gin.Context)  {
	ns := c.Query("namespace")
	podName := c.Query("name")
	fmt.Println(podName)

	////b2 := make([]byte,1024*1024)   //1M
	//b2 := make([]byte,0)
	//c.Request.Body.Read(b2)
	//fmt.Println("b2 is" , string(b2))

	body, _ := ioutil.ReadAll(c.Request.Body)

	err := k8s.UpdateKubernetesPodMetadata(ns,podName,body)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code": 400,
			"message": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
	})

}


func GetK8sConfigmap(c *gin.Context)  {
	ns := c.Query("namespace")

	name := c.Query("name")

	configMap,_ := k8s.GetConfigmapDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"configMap":configMap,

	})
}

func GetK8sSecret(c *gin.Context)  {
	ns := c.Query("namespace")

	name := c.Query("name")

	secret,_ := k8s.GetSecretDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"secret":secret,

	})
}

func GetK8sJob(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")
	job,_ := k8s.GetJobDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"job":job,
	})
}
func GetK8sStatefulset(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")
	statefulsets,_ := k8s.GetStatefulsetDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"statefulsets":statefulsets,
	})
}



func GetK8sClusterRoles(c *gin.Context)  {
	roles,_ := k8s.GetClusterRole()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"clusterroles":roles,

	})
}

func GetK8sPersistentVolumeList(c *gin.Context)  {
	pvs,_ := k8s.GetKubernetesPersistentVolumeList()
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"persistentVolume":pvs,
	})
}

func GetK8sPersistentVolume(c *gin.Context)  {
	name := c.Query("name")
	pv,_ := k8s.GetKubernetesPersistentVolume(name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"persistentVolume":pv,
	})
}

func GetK8sPersistentVolumeClaim(c *gin.Context)  {

	ns := c.Query("namespace")
	name := c.Query("name")

	pvc,_ := k8s.GetKubernetesPersistentVolumeClaim(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"persistentVolumeClaim":pvc,
	})
}



func GetK8sDaemonsetDetail(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")

	ds,pods,services,events,_ :=k8s.GetKubernetesDaemonsetDetail(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"daemonset":ds,
		"pods":pods,
		"events":events,
		"services":services,

	})
}

func GetK8sPodLogDetail(c *gin.Context)  {
	ns := c.Query("namespace")
	name := c.Query("name")

	logLines,_ := k8s.GetKubernetesPodLogs(ns,name)
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"logs":logLines,
	})
}



func DeleteK8sResource(c *gin.Context)  {
	ns := c.Query("namespace")
	resource := c.Query("name")
	resourceType := c.Query("type")
	err := k8s.DeleteResource(ns,resource,resourceType)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code": 400,
			"message": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
	})

}

func GetK8sResource(c *gin.Context)  {
	ns := c.Query("namespace")
	resource := c.Query("name")
	resourceType := c.Query("type")
	meta,err := k8s.GetResource(ns,resource,resourceType)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code": 400,
			"message": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
		"metadata":meta,
	})

}


func UpdateK8sResourceMetadata(c *gin.Context)  {
	ns := c.Query("namespace")
	resource := c.Query("name")
	resourceType := c.Query("type")

	////b2 := make([]byte,1024*1024)   //1M
	//b2 := make([]byte,0)
	//c.Request.Body.Read(b2)
	//fmt.Println("b2 is" , string(b2))

	body, _ := ioutil.ReadAll(c.Request.Body)

	err := k8s.PutResource(ns,resource,resourceType,body)
	if err != nil {
		c.JSON(http.StatusOK,gin.H{
			"code": 400,
			"message": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK,gin.H{
		"code": 200,
	})

}